<?php

/**
 * @desc Application request in command-line mode. 
 *
 */
class Kernel_Request_Cli extends Kernel_Request_Abstract
{

    /**
     * @desc Request params. 
     *
     * @param array
     */
    private $_params = array();

    public function __construct() 
    {
        $argc = $_SERVER['argc'];
        $argv = $_SERVER['argv'];

        // if arguments are passed (first array element is file name)
        if ($argc > 1) {
            for ($i = 1; $i < $argc; $i++) {

                // if it's param name
                if ($this->_isParamName($argv[$i])) 
                {
                    // and after we have another agrument
                    if (($argc > $i + 1)
                    // and it's NOT param name
                    && !$this->_isParamName($argv[$i + 1])) 
                    {
                        // push param and its value into stack
                        $this->_params[substr($argv[$i], 1)] = $argv[++$i];
                    } 
                    else 
                    {
                        // else push into stack only param name
                        $this->_params[$argv[$i]] = true;
                    }
                }
            }
        }
    }

    /**
     * @desc Return param value.
     *
     * @param   string  $param
     * @param   mixed   $default
     * @return  mixed
     */
    public function get($param, $default = null) 
    {
        return get_array_element($this->_params, $param, $default);
    }

    /**
     * @desc Return whether specified param is passed. 
     *
     * @param   string  $param
     * @param   boolean $empty
     * @return  boolean
     */
    public function has($param, $empty = false) 
    {
        return $empty ? isset($this->_params[$param]) : !empty($this->_params[$param]);
    }

    /**
     * @desc Return whether specified argument is param name.
     *
     * @param   string   $arg
     * @return  boolean
     */
    private function _isParamName($arg) 
    {
        return '-' == substr($arg, 0, 1);
    }
    
}
